package com.example.petstore.controller;

import com.example.petstore.common.CommonResponse;
import com.example.petstore.domain.Category;
import com.example.petstore.domain.Product;
import com.example.petstore.service.CategoryService;
import com.example.petstore.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ProductService productService;

    /**
     * 进入product展示页面
     * @param categoryId
     * @param model
     * @return
     */
    @RequestMapping("/categoryForm")
    public String categoryForm(String categoryId, Model model){
        CommonResponse commonResponse = categoryService.getCategoryByCategoryId(categoryId);
        model.addAttribute("category", commonResponse.getData());
        List<Product> productList = (List<Product>) productService.getProductListByCategory(categoryId).getData();
        model.addAttribute("productList", productList);
        log.info("用户查看 categoryId " + categoryId);
        return "catalog/category";
    }
}
